<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DayController extends Controller
{
		//后台日记列表 
	public function getList(Request $request){
		// return 2222;
		// 获取分页条数
		$num=$request->input('num',10);
		// 判断是否有关键字
		if($request->input('keywords')){
			$cates=DB::table('day')->join('user','user.id','=','day.uid')->select('day.*','user.username as uname')->where('day.title','like','%'.$request->input('keywords').'%')->paginate($num);
		}else{
			$cates=DB::table('day')->join('user','user.id','=','day.uid')->select('day.*','user.username as uname')->paginate(10);
		}
		$all=$request->all();
		return view('admin.day.list',['cates'=>$cates,'request'=>$all]);
	}
	// 后台日记删除
	public function postDelete(Request $request){
		$id=$request->input('id');
		// dd($id);
		$res=DB::table('day')->where('id',$id)->delete();
		if($res){
			return 1;
		}else{
			return 2;
		}
	}
	// 后台日记修改显示
	public function getEdit($id){
		$cates=DB::table('day')->join('user','user.id','=','day.uid')->select('day.*','user.username as uname')->where('day.id',$id)->first();
		return view('/admin/day/edit',['cates'=>$cates]);
	}
	// 后台日记修改
	public function postUpdate(Request $request){

		$cates=$request->only('id','title');
		$cates['content']=$_POST['content'];
		$id=$request->input('id');
		$res=DB::table('day')->where('id',$id)->update($cates);
		if($res){
			return 1;
		}else{
			return 2;
		}
	}

	// 前台日记管理
	public function getAdd(Request $request){
		$cates=DB::table('day')->join('user','user.id','=','day.uid')->select('day.*','user.username as uname')->first();
		// echo 222;
		return view('/home/day/add',['cates'=>$cates]);
	}

	// 前台日记插入
	public function postInsert(Request $request){
		 $cates=$request->only('uid','title','content','dtime');
		 // $cates['dtime']=time();
		 $cates['dtime']=date("Y-m-d H:i:s",time());
		 // dd($cates);
		 $id=$request->input('uid');
		 // 执行插入
		 $res=DB::table('day')->where('uid',$id)->insert($cates);
		 // dd($cates,$res);
		 // 判断是否插入成功
		 if($res){
    		return redirect('/admin/day/show')->with('success','日记写入成功');
    	}else{
    		return redirect('/admin/day/add')->with('success','日记写入失败');
    	}
	}
	// 前台日记详情显示页
	public function getShow(Request $request){
		// 查询所有日记
		$uid = session('id');
		$cates=DB::table('day')->join('user','day.uid','=','user.id')->where('day.uid',$uid)->select('day.*','user.username','user.pic')->paginate(4);
		// dd($cates);
		// 该用户的最新回应
		$newhf=DB::table('comment')->where('uid',$uid)->join('user','comment.uid','=','user.id')->select('comment.*','user.username','user.pic')->orderBy('comment.id')->first();
		// dd($newhf);
		return view('/home/day/show',['cates'=>$cates,'newhf'=>$newhf]);
	}
	// 前台日记显示修改
	public function getUpdate($id){
		// dd($id);
		$cates=DB::table('day')->join('user','user.id','=','day.uid')->select('day.*','user.username as uname')->where('day.id',$id)->first();
		// dd($cates);
	 	return view('/home/day/update',['cates'=>$cates]);
	}

	// 前台日记修改
	public function postGeng(Request $request){
		$cates=$request->only('id','title','content');
		$id=$request->input('id');
		// dd($cates,$id,$request->all());
		$res=DB::table('day')->where('id',$id)->update($cates);
		if($res){
    		return redirect('/home/day/show')->with('success','日记修改成功');
    	}else{
    		return redirect('/home/day/show')->with('success','日记修改失败');
    	}
	}
	// 前台日记删除
	public function getShanchu($id){
		
		$uid=DB::table('day')->where('id',$id)->select('uid')->first();
		$res=DB::table('day')->where('id',$id)->delete();
		// dd($uid);
		if($res){
    		return redirect('/home/day/show/$uid')->with('success','日记删除成功');
    	}else{
    		return redirect('/home/day/show')->with('error','日记删除失败');
    	}
	}

	// 验证码
	public function getVcode(){
		ob_clean();//清除输出缓存区的内容
	    //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Vcode'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
        die;

	}

	// 前台日记回复显示
	public function getHf($id){
			// 显示日记的主要信息
			$uid=session('id');
			if($uid){
				$res=DB::table('day')->where('id',$id)->first();
			$uid=$res->uid;
			// 显示用户信息
			$user=DB::table('user')->where('id',$uid)->first();
			// 显示最近一条日记
			$day=DB::table('day')->where('uid',$uid)->orderBy('id','desc')->take(1)->get();
			// 显示回复
			$hf=DB::table('hf')->join('user','hf.uid','=','user.id')->select('hf.*','user.pic','user.username')->where('did',$id)->take(5)->get();
			$num=DB::table('hf')->where('did',$id)->count();
			// dd($hf);
			return view('home.day.hf',['res'=>$res,'user'=>$user,'day'=>$day,'hf'=>$hf,'num'=>$num]);
		}else{
			return view('home.login.login');
		}
		
	}

	// 前台日记回复
	public function getInsert(Request $request){
		// echo 2222;
		$uid=$request->input('uid');
		// dd($uid);
		$cate=$request->only('uid','did','content');
		$id=$request->input('did');
		// dd($id);
		 $cate['time']=date("Y-m-d H:i:s",time());
		$res=DB::table('hf')->insert($cate);
		// dd($res);
		// return view('home.day.hf',['id'=>$id]);
		 $res = $_SERVER['HTTP_REFERER']; 
		 // dd($res);
		return redirect($res);

	}

	public function getDelete(Request $request){
		// dd($request->all());
		$id=$request->input('id');
		// dd($id);
		$res=DB::table('hf')->where('id',$id)->delete();
		// dd($res);
		if($res){
			$res1 = $_SERVER['HTTP_REFERER']; 
		 	// dd($res);
			return redirect($res1);

		}else{

		}
	}

/*	// 前台日记显示
	public function getXq($id){
		// dd($id);
		$res=DB::table('day')->join('user','day.uid','=','user.id')->where('day.id',$id)->select('day.*','user.pic','user.username')->first();
		// 显示回复
			$hf=DB::table('hf')->join('user','hf.uid','=','user.id')->select('hf.*','user.pic','user.username')->where('did',$id)->take(5)->get();
			$num=DB::table('hf')->where('did',$id)->count();
		// dd($res,$hf);
		return view('home.day.xq',['res'=>$res,'hf'=>$hf,'num'=>$num]);
	}*/

}
